package com.music.action.genres;

import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.music.action.BaseAction;
import com.music.action.BaseKey;
import com.music.common.ParamUtil;
import com.music.domain.CateMusic;
import com.music.domain.Music;
import com.music.domain.PageInfo;
import com.music.genres.service.FindCateMusicService;
import com.music.genres.service.FindMusicByParamService;
import com.mysql.fabric.xmlrpc.base.Param;

/**
 * 我的音乐页面
 * 
 * @author LFSenior
 *
 */
@SuppressWarnings("serial")
public class GenresAction extends BaseAction {
	@Autowired
	FindCateMusicService findCateMusicService;

	@Autowired
	FindMusicByParamService findMusicByCateService;

	/**
	 * 跳转到我的音乐页面
	 * 
	 * @return
	 */
	@Action(value = "genres", results = { @Result(name = "genres", location = "genres.jsp") })
	public String redicterGenres() {
		request.put(BaseKey.CATE_MUSIC, findCateMusic());
		return "genres";
	}

	@Action(value = "findMusicByCate", results = { @Result(name = "musicItem", location = "genres/musicitem.jsp") })
	public String findMusicByCate() throws Exception {
		reParam.clear();
		/* 获取相关数据 */
		Map<String, Object> param = ParamUtil.buildRequest(ServletActionContext.getRequest());
		PageInfo pageInfo = (PageInfo) session.get(BaseKey.PAGE);
		if(pageInfo==null){
			pageInfo=new PageInfo();
			pageInfo.setCurr_total("1");
		}
		request.put(BaseKey.CATE_MUSIC_CONTENT, findMusicByCate(param.get("cate").toString(),pageInfo));
		session.put(BaseKey.PAGE, pageInfo);
		return "musicItem";
	}

	/**
	 * 根据类别查找音乐
	 * @param cate
	 * @param pageInfo 
	 * @return
	 */
	private List<Music> findMusicByCate(String cate, PageInfo pageInfo) {
		return findMusicByCateService.findMusicByCate(cate,pageInfo);
	}

	/**
	 * 查询音乐类别
	 * 
	 * @return
	 */
	public List<CateMusic> findCateMusic() {
		return findCateMusicService.findCateMusic();
	}
	
	/**
	 * 上一页
	 * @return
	 * @throws Exception 
	 */
	@Action(value = "perpage", results = { @Result(name = "perpage", location = "genres/musicitem.jsp") })
	public String perPage() throws Exception{
		PageInfo pageInfo = (PageInfo) session.get(BaseKey.PAGE);
		Map<String, Object> param = ParamUtil.buildRequest(ServletActionContext.getRequest());
		if(pageInfo==null){
			pageInfo=new PageInfo();
			pageInfo.setCurr_total("1");
		}
		pageInfo.setCurr_total(String.valueOf(Integer.parseInt(pageInfo.getCurr_total())-1));
		if(Integer.parseInt(pageInfo.getCurr_total())<=0){
			pageInfo.setCurr_total("1");
		}
		request.put(BaseKey.CATE_MUSIC_CONTENT, findMusicByCate(param.get("cate").toString(),pageInfo));
		session.put(BaseKey.PAGE, pageInfo);
		return "perpage";
	}
	
	
	/**
	 * 下一页
	 * @return
	 * @throws Exception 
	 */
	@Action(value = "nextpage", results = { @Result(name = "nextpage", location = "genres/musicitem.jsp") })
	public String nextPage() throws Exception{
		PageInfo pageInfo = (PageInfo) session.get(BaseKey.PAGE);
		Map<String, Object> param = ParamUtil.buildRequest(ServletActionContext.getRequest());
		if(pageInfo==null){
			pageInfo=new PageInfo();
			pageInfo.setCurr_total("1");
		}
		pageInfo.setCurr_total(String.valueOf(Integer.parseInt(pageInfo.getCurr_total())+1));
		if(Integer.parseInt(pageInfo.getCurr_total())>=Integer.parseInt(pageInfo.getTotal())){
			pageInfo.setCurr_total(pageInfo.getTotal());
		}
		request.put(BaseKey.CATE_MUSIC_CONTENT, findMusicByCate(param.get("cate").toString(),pageInfo));
		session.put(BaseKey.PAGE, pageInfo);
		return "nextpage";
	}

}
